<%@ page language="java" contentType="text/html; charset=utf-8"
    pageEncoding="utf-8"%>
<%
    // 项目上下文路径
    String contextPath = request.getContextPath();
%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Insert title here</title>
</head>
<body>
<h1>Hellow World!!!</h1>
<input id="base64"  type="button" onclick="tset()"/>
<img alt="gujianhua" src="<%=contextPath%>/images/jinggao.png">
</body>
<script>
    function getBase64Image(img) {
        var canvas = document.createElement("canvas");
        canvas.width = img.width;
        canvas.height = img.height;

        var ctx = canvas.getContext("2d");
        ctx.drawImage(img, 0, 0, img.width, img.height);
        var ext = img.src.substring(img.src.lastIndexOf(".")+1).toLowerCase();
        var dataURL = canvas.toDataURL("image/"+ext);
        return dataURL;
    }
    function  tset() {
        var img = "";
        var image = new Image();
        image.crossOrigin = '';
        image.src = img;
        image.onload = function(){
            var base64 = getBase64Image(image);
            console.log(base64);
        }
    }
</script>
</html>